package cn.qhuan.modules.service.impl;

import cn.qhuan.modules.constos.PageResponseResult;
import cn.qhuan.modules.constos.StatusConstos;
import cn.qhuan.modules.dtos.DocumentAuthDto;
import cn.qhuan.modules.dtos.DocumentDto;
import cn.qhuan.modules.dtos.DocumentShowDto;
import cn.qhuan.modules.dtos.DocumentDtoVo;
import cn.qhuan.modules.entity.Document;
import cn.qhuan.modules.entity.Special;
import cn.qhuan.modules.mapper.DocumentMapper;
import cn.qhuan.modules.mapper.SpecialMapper;
import cn.qhuan.modules.mapper.TagMapper;
import cn.qhuan.modules.service.IDocumentService;

import cn.qhuan.modules.vo.DocumentVO;
import cn.qhuan.modules.entity.Special;
import cn.qhuan.modules.mapper.SpecialMapper;
import cn.qhuan.modules.entity.Tag;
import cn.qhuan.modules.mapper.TagMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 文档表
 * @Author: 司翔宇
 * @Date:   2020-12-21
 * @Version: V1.0
 */
@Service
public class DocumentServiceImpl extends ServiceImpl<DocumentMapper, Document> implements IDocumentService {

    @Autowired
    private DocumentMapper documentMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private SpecialMapper specialMapper;
    @Override
    public Result findAll(DocumentDto documentDto) {
        //1.检查参数
        if (documentDto == null){
            return  Result.error("缺少参数");
        }
        //分页参数检查
        documentDto.checkParam();
        //2.分页条件查询
        IPage pageParam = new Page(documentDto.getPage(), documentDto.getSize());
        LambdaQueryWrapper<Document> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        //2.1按照关键字进行查询
        if (StringUtils.isNotBlank(documentDto.getKeyWord())){
            lambdaQueryWrapper.like(Document::getTitle,documentDto.getKeyWord());
        }
        //2.2按照审核状态进行查询
        if (StringUtils.isNotBlank(documentDto.getStatus())){
            lambdaQueryWrapper.eq(Document::getStatus,documentDto.getStatus());
        }
        //2.3按照文档格式进行精准查询.xml
        if (StringUtils.isNotBlank(documentDto.getType())){
            lambdaQueryWrapper.eq(Document::getType,documentDto.getType());
        }
        IPage pageList = page(pageParam, lambdaQueryWrapper);
        PageResponseResult responseResult =new PageResponseResult(documentDto.getPage(),documentDto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        return responseResult;
    }

    @Override
    public Result<?> updateStatus(DocumentAuthDto dto, Integer type) {
        //1.检查参数
        if (dto == null || dto.getId() ==null){
            return Result.error("缺少参数");
        }
        //2.查询文档信息
        Document document = documentMapper.selectById(dto.getId());
        if (document == null){
            return Result.error("数据不存在");
        }
        //3.审核失败
        if (type.equals(StatusConstos.STATUS_FAIL)){
            updateDocument(document,StatusConstos.STATUS_FAIL,dto.getMsg());
        }else if (type.equals(StatusConstos.STATUS_PASS)){
            updateDocument(document,StatusConstos.STATUS_PASS,"审核通过");
        }
        return Result.OK("修改成功");
    }

    @Override
    public Result<?> findDocumentIdsBySpecialId(String id) {
        //1.检查参数
        if ( id == null ){
            return Result.error("缺少参数");
        }
        List<String> list = documentMapper.finddocumentIdsBySpecialId(id);
        List<Document> docList =new ArrayList<>();
        Document document =new Document();
        for (String s : list) {
            document =documentMapper.selectById(s);
            docList.add(document);
        }
        return Result.OK(docList);

    }

    @Override
    public Result<?> addDocToExhi(String uuid, String docId, String exhiId) {
        if (uuid == null || docId == null || exhiId == null ){
            return Result.error("缺少参数");
        }
        if (documentMapper.selectDocumentCountToExhi(docId) == 0){
            documentMapper.addDocToExhi(uuid,docId,exhiId);
            return Result.OK("展厅设置成功");
        }else {
            documentMapper.deleteExhiAssociation(docId);
            documentMapper.addDocToExhi(uuid,docId,exhiId);
            return Result.OK("展厅修改成功");
        }

    }

    @Override
    public Result<?> addDocToSpec(String uuid, String docId, String specId) {
        if (uuid == null || docId == null || specId == null ){
            return Result.error("缺少参数");
        }
        if (documentMapper.selectDocumentCountToSpecial(docId) == 0){
            documentMapper.addDocToSpec(uuid,docId,specId);
            return Result.OK("专题设置成功");
        }else {
            documentMapper.deleteSpecialAssociation(docId);
            documentMapper.addDocToSpec(uuid,docId,specId);
            return Result.OK("专题修改成功");
        }
    }

    @Override
    public Result<?> updateShowStatus(DocumentShowDto showDto, Integer type) {
        //1.检查参数
        if (showDto == null || showDto.getId() == null ){
            return Result.error("缺少参数");
        }
        //2.查询文档信息
        Document document = documentMapper.selectById(showDto.getId());
        if (document == null ){
            return Result.error("文档不存在");
        }
        //修改文档展示状态
        //不显示
        if (type.equals(StatusConstos.HIDDEN_STATUS)){
            updateShowDoc(document,StatusConstos.HIDDEN_STATUS);
        }else if (type.equals(StatusConstos.SHOW_STATUS)){
            updateShowDoc(document,StatusConstos.SHOW_STATUS);
        }
        return Result.OK("修改成功");
    }

    @Override
    public Result<?> addDocToType(String uuid, String docId, String typeId) {
        if (uuid == null || docId == null || typeId == null ){
            return Result.error("缺少参数");
        }
        if (documentMapper.selectDocumentCountToType(docId) == 0){
            documentMapper.addDocToType(uuid,docId,typeId);
            return Result.OK("类别设置成功");
        }else {
            documentMapper.deleteTypeAssociation(docId);
            documentMapper.addDocToType(uuid, docId, typeId);
            return Result.OK("类别修改成功");
        }

    }

    @Override
    public Result<?> findDocumentIdsByTypeId(String id) {
        //1.检查参数
        if ( id == null ){
            return Result.error("缺少参数");
        }
        List<String> list = documentMapper.findDocIdsByClassId(id);
        List<Document> docList =new ArrayList<>();
        Document document =new Document();
        for (String s : list) {
            document = documentMapper.selectById(s);
            docList.add(document);
        }
        return Result.OK(docList);
    }

    @Override
    public Result<?> findDocumentIdsByExhiId(String id) {
        //1.检查参数
        if (id == null ){
            return Result.error("缺少参数");
        }
        List<String> list = documentMapper.findDocIdsByEhxhiId(id);
        List<Document> docList =new ArrayList<>();
        Document document =new Document();
        for (String s : list) {
            document =documentMapper.selectById(s);
            docList.add(document);
        }
        return Result.OK(docList);
    }

    @Override
    public Result<?> addDocToArtist(String uuid, String docId, String artistId) {
        //1.检查参数
        if (uuid == null || docId == null || artistId == null ){
            return Result.error("缺少参数");
        }
        if (documentMapper.selectDocumentCountToArtist(docId) == 0){
            documentMapper.addDocToArtist(uuid, docId, artistId);
            return  Result.OK("艺术家设置成功");
        }else {
            documentMapper.deleteArtistAssociation(docId);
            documentMapper.addDocToArtist(uuid, docId, artistId);

            return Result.OK("艺术家修改成功");
        }
    }

    @Override
    public Result<?> addDocToTag(String uuid, String docId, String tagId) {

        //1.检查参数
        if (uuid == null || docId == null || tagId == null ){
            return Result.error("缺少参数");
        }
        if (documentMapper.selectDocumentCountToTag(docId) == 0){
            documentMapper.addDocToTag(uuid, docId, tagId);
            return  Result.OK("标签设置成功");
        }else {

            documentMapper.deleteTagAssociation(docId);
            documentMapper.addDocToTag(uuid, docId, tagId);
            return Result.OK("标签修改成功");
        }
    }

    @Override
    public Result<?> selectDocumentCount() {
        Integer count = documentMapper.selectDocumentCount();
        if (count >0){
            return Result.OK(count);
        }else {
            return Result.error("暂无文档");
        }

    }

    @Override
    public DocumentVO findDocumentVo(Integer id) {
        //查询文档信息
        Document document = getById(id);

        //查询标签
        Tag tag= null ;
        if (id != null ){
            tag = tagMapper.findByDocumentId(id.toString());
        }
        //查询专题
        Special qhSpecial = null ;
        if ( id != null){
            qhSpecial= specialMapper.findByDocumentId(id.toString());
        }
        //封装vo结果进行返回
        DocumentVO documentVO=new DocumentVO();
        BeanUtils.copyProperties(document,documentVO);
        if ( qhSpecial != null && tag != null ){

            documentVO.setTagName(tag.getName());
            documentVO.setSpecialName(qhSpecial.getName());

        }

       return documentVO;
    }

    @Override
    public DocumentVO getAfterDocument(Integer documentId) {

        Document document = new Document();
        //
       if (documentMapper.getAfterDocument(documentId.toString()).size()>0){
           document =documentMapper.getAfterDocument(documentId.toString()).get(0);
       }else {
           document.setTitle("这是最后一篇文档");
           document.setId(documentId.toString());
       }

        //查询标签
        Tag tag = null ;
        if (documentId != null){
            tag = tagMapper.findByDocumentId(documentId.toString());
        }
        //查询专题
        Special qhSpecial = null ;
        if ( documentId != null){
            qhSpecial= specialMapper.findByDocumentId(documentId.toString());
        }
        //封装vo结果进行返回
        DocumentVO documentVO=new DocumentVO();
        BeanUtils.copyProperties(document,documentVO);
        if ( qhSpecial != null && tag != null ){
            documentVO.setTagName(tag.getName());
            documentVO.setSpecialName(qhSpecial.getName());

        }
        return documentVO;
    }

    @Override
    public DocumentVO getPreDocument(Integer documentId) {
        Document document = new Document();
        //进行返回集合长度的判断
        if (documentMapper.getPreDocument(documentId.toString()).size()>0){
            //如果>0 代表还有文档
            document =documentMapper.getPreDocument(documentId.toString()).get(0);
        }else {
            //如果没有 则代表当前已经是第一篇文档
            document.setTitle("这是第一篇文档");
            document.setId(documentId.toString());
        }

        //查询标签
        Tag tag = null ;
        if (documentId != null){
            tag = tagMapper.findByDocumentId(documentId.toString());
        }
        //查询专题
        Special qhSpecial = null ;
        if ( documentId != null){
            qhSpecial= specialMapper.findByDocumentId(documentId.toString());
        }
        //封装vo结果进行返回
        DocumentVO documentVO=new DocumentVO();
        BeanUtils.copyProperties(document,documentVO);
        if ( qhSpecial != null && tag != null ){
            documentVO.setTagName(tag.getName());
            documentVO.setSpecialName(qhSpecial.getName());

        }
        return documentVO;
    }

    @Override
    public Result<?> addDocument(DocumentDtoVo dto) {
        //判断参数
        if (dto != null ){
            save(dto);
            addDocToTag(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getTagId());
            addDocToSpec(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getSpecialId());
            addDocToType(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getTypeId());
            addDocToExhi(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getExhiId());
            return Result.OK("添加成功");
        }

        return Result.error("添加失败");
    }

    @Override
    public Result<?> editDocument(DocumentDtoVo dto) {
        //判断参数
        if ( dto == null || dto.getId() == null){
            return Result.error("缺少参数");
        }
        updateById(dto);
        addDocToTag(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getTagId());
        addDocToSpec(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getSpecialId());
        addDocToType(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getTypeId());
        addDocToExhi(String.valueOf(UUID.randomUUID()),dto.getId(),dto.getExhiId());
        return Result.OK("编辑成功");
    }

    /**
     * 修改文档状态信息
     * @param document
     * @param status
     * @param msg
     */
    private void updateDocument (Document document, Integer status, String msg){
        document.setStatus(String.valueOf(status));
        document.setRefusedReason(msg);
        documentMapper.updateById(document);

    }
    /**
     * 修改文档是否在前台显示状态
     * @param document
     * @param showStatus
     */
    private void updateShowDoc(Document document, Integer showStatus){
        document.setShowFront(showStatus.toString());
        documentMapper.updateById(document);
    }
}
